package com.clerkq.Scroller
{
	import com.content.model.LoadedAssets.SkinProxy;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
    public class Scroller extends UIObject implements IDisposable {

        private var _bg:Sprite;
        private var _slider:DragBlock;
        private var _arrow1:Arrows;
        private var _arrow2:Arrows;
        
        private var _scrollerSize:Number;	// ScrollBar的高度
        private var _viewSize:Number;	// 图片显示容器的 "高" 或 "宽"
        private var _realWidth:Number;	// 实际图片的 "宽"
        private var _realHeight:Number;	// 实际图片的 "高"
        private var _scrollManager:ScrollerManager;
        private var _content:DisplayObject;
        private var _dragRect:Rectangle;	// 这个是可以拖动的区域，是一个限制值
        private var _contentRect:Rectangle;
        
        private var _direction:String = ScrollerType.VERTICAL;
        
        public function Scroller(scrollerSize:Number, viewSize:Number, realWidth:Number, realHeight:Number, content:DisplayObject, direction:String){
            _content = content;
            _scrollerSize = scrollerSize;
            _viewSize = viewSize;
            _realWidth = realWidth;
            _realHeight = realHeight;
            _direction = direction;
            
			AddBg();
			AddArrows();
			AddDragBlock();
			PosElements();
			CreateManager();
        }
		
        private function AddBg():void{
            _bg = new SkinProxy.IMG_SSliderBG() as Sprite;
            var width:Number = (_direction == ScrollerType.VERTICAL) ? Arrows.WIDTH : 1;
            var height:Number = (_direction == ScrollerType.VERTICAL) ? 1 :Arrows.HEIGHT;
            _bg.width = width;
            _bg.height = height;
            _bg.alpha = 0.6;
            addChild(_bg);
            
            _dragRect = new Rectangle(0, 0, 0, 0);
        }
        private function AddDragBlock():void{
            _slider = new DragBlock(_direction);
            _slider.addEventListener(DragBlock.ON_PRESS_SLIDER, onPressSlider);
            _slider.addEventListener(DragBlock.ON_RELEASE_SLIDER, onReleaseSlider);
            addChild(_slider);
            
            if(_direction == ScrollerType.VERTICAL){
            	_slider.y = Arrows.HEIGHT;
            }else{
            	_slider.x = Arrows.WIDTH;
            }
        }
        private function AddArrows():void{
            _arrow1 = new Arrows(0, _direction);
            addChild(_arrow1);
            _arrow2 = new Arrows(1, _direction);
            addChild(_arrow2);
        }
        
        private function PosElements():void{
        	
        	var ratio:Number;
        	if(_direction == ScrollerType.VERTICAL){
        		// bg
        		_bg.x = 0;
        		_bg.y = Arrows.HEIGHT;
	            _bg.height = _scrollerSize - _arrow1.height*2;
	            // arrow
	            _arrow2.y = _bg.y + _bg.height// + 5;
	            
	            // slider height
	            ratio = _viewSize / _realHeight;
	            var sliderHeight:Number = int( (_scrollerSize - _arrow1.height*2) * ratio );
	            _slider.sliderHeight = sliderHeight;
	            
	            // dragRect
	            _dragRect.height = _bg.height - sliderHeight;
	            _dragRect.y = _bg.y;
	            _dragRect.x = _bg.x;
        	}else{
        		_bg.x = Arrows.WIDTH;
        		_bg.y = 0;
	            _bg.width = _scrollerSize - _arrow1.width*2;
	            _arrow2.x = _bg.x + _bg.width// + 5;
	            
	            ratio = _viewSize / _realWidth;
	            var sliderWidth:int = int( (_scrollerSize - _arrow1.width*2) * ratio );
	            _slider.sliderWidth = sliderWidth;
	            
	            _dragRect.y = _bg.y;
	            _dragRect.width = _bg.width - sliderWidth;
	            _dragRect.x = _bg.x;
        	}
        	
        	_contentRect = new Rectangle(0,0, _realWidth - _viewSize, _realHeight - _viewSize);
        }
        
        private function CreateManager():void{
            _scrollManager = new ScrollerManager(	_slider, 
            										_dragRect, 
            										_content, 
            										_viewSize, 
            										_realWidth,
            										_realHeight,
            										_direction);
			
            _slider.addEventListener(DragBlock.ON_PRESS_SLIDER, _scrollManager.onPressSlider);
            _slider.addEventListener(DragBlock.ON_RELEASE_SLIDER, _scrollManager.onReleaseSlider);
            _arrow1.addEventListener(Arrows.ON_PRESS_ARROW, _scrollManager.onPressArrow);
            _arrow1.addEventListener(Arrows.ON_RELEASE_ARROW, _scrollManager.onReleaseArrow);
            _arrow2.addEventListener(Arrows.ON_PRESS_ARROW, _scrollManager.onPressArrow);
            _arrow2.addEventListener(Arrows.ON_RELEASE_ARROW, _scrollManager.onReleaseArrow);
        }
        
        // 让 Slider 归 0 位置
		private function ResetSliderPos():void{
			if(_direction == ScrollerType.VERTICAL){
				_slider.y = _dragRect.y;
				_slider.x = _bg.x;
			}else{
				_slider.x = _dragRect.x;
				_slider.y = _bg.y;
			}
		}
        
        // Events
        private function onReleaseSlider(evt:Event):void{
            _slider.stopDrag();
        }
        private function onPressSlider(evt:Event):void{
            _slider.startDrag(false, _dragRect);
        }
		
		// public //////////////////////////////////
		public function SetSize(scrollerSize:Number, viewSize:Number, realSize:Number):void{
			
			if(_direction == ScrollerType.VERTICAL){
				_realHeight = realSize;
				if(viewSize >= _realHeight){
					this.visible = false;
				}else{
					this.visible = true;
				}
			}else{
				_realWidth = realSize;
				if(viewSize >= _realWidth){
					this.visible = false;
				}else{
					this.visible = true;
				}
			}
			
			_viewSize = viewSize;
			_scrollerSize = scrollerSize;
			PosElements();
			_scrollManager.setSize(_dragRect, _viewSize, realSize);
		}
		
		public function reset():void{
            ResetSliderPos();
            _scrollManager.reset();
        }
		
		// 根据 content 更新 slider 的位置
		public function updateSliderPos():void{
			if(_direction == ScrollerType.VERTICAL){
				_slider.y = _dragRect.y - (_dragRect.height) * _content.y / _contentRect.height;
			}else{
				_slider.x = _dragRect.x - (_dragRect.width) * _content.x / _contentRect.width;
			}
		}

		public function Dispose():void{
			_slider.removeEventListener(DragBlock.ON_PRESS_SLIDER, onPressSlider);
            _slider.removeEventListener(DragBlock.ON_RELEASE_SLIDER, onReleaseSlider);
            
			_slider.removeEventListener(DragBlock.ON_PRESS_SLIDER, _scrollManager.onPressSlider);
            _slider.removeEventListener(DragBlock.ON_RELEASE_SLIDER, _scrollManager.onReleaseSlider);
            _arrow1.removeEventListener(Arrows.ON_PRESS_ARROW, _scrollManager.onPressArrow);
            _arrow1.removeEventListener(Arrows.ON_RELEASE_ARROW, _scrollManager.onReleaseArrow);
            _arrow2.removeEventListener(Arrows.ON_PRESS_ARROW, _scrollManager.onPressArrow);
            _arrow2.removeEventListener(Arrows.ON_RELEASE_ARROW, _scrollManager.onReleaseArrow);
            
            _scrollManager.Dispose();
            _slider.Dispose();
            _arrow1.Dispose();
            _arrow2.Dispose();
		}
		
        public function get scrollManager():ScrollerManager	{      return _scrollManager;       }
        
    }
}